﻿using System.Collections.Generic;

namespace MediaAssistant.DAL
{
    public partial class LibraryItem
    {
        public LibraryItem()
        {
            HasChildren = false;
        }

        public IEnumerable<LibraryItem> OrderedChildren
        {
            get
            {
                return DatabaseManager.Instance.GetOrderedChildren(this);
            }
        }


        private bool _isExpanded;

        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                _isExpanded = value;
                OnPropertyChanged("IsExpanded");
            }
        }

        public int Lavel { get; set; }

        public IEnumerable<Music> GetAllMusic()
        {
            var musics = new List<Music>();
            AddMusicsRecursively(musics, this);
            return musics;
        }

        private static void AddMusicsRecursively(ICollection<Music> musics, LibraryItem libraryItem)
        {
            foreach (var music in libraryItem.Musics)
            {
                if(musics.Contains(music)==false)
                    musics.Add(music);
            }
            foreach (var child in libraryItem.Children)
            {
                AddMusicsRecursively(musics, child);
            }
        }

        public IEnumerable<Movie> GetAllMovies()
        {
            var movies = new List<Movie>();
            AddMovieRecursively(movies, this);
            return movies;
        }

        private static void AddMovieRecursively(ICollection<Movie> movies, LibraryItem libraryItem)
        {
            foreach (var music in libraryItem.Movies)
            {
                if (movies.Contains(music) == false)
                    movies.Add(music);
            }
            foreach (var child in libraryItem.Children)
            {
                AddMovieRecursively(movies, child);
            }
        }
        public override string ToString()
        {
            return Title;
        }

        public bool IsEmpty()
        {
            return Children.Count==0 && Movies.Count==0 && Musics.Count==0;
        }
    }
}
